package cn.iocoder.yudao.module.shop.service.shopInfo;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.shop.controller.admin.shopInfo.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.shopInfo.ShopInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.shop.dal.mysql.shopInfo.ShopInfoMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

/**
 * 商户信息 Service 实现类
 *
 * @author 吕小文
 */
@Service
@Validated
public class ShopInfoServiceImpl implements ShopInfoService {

    @Resource
    private ShopInfoMapper shopInfoMapper;

    @Override
    public Long createShopInfo(ShopInfoSaveReqVO createReqVO) {
        // 插入
        ShopInfoDO shopInfo = BeanUtils.toBean(createReqVO, ShopInfoDO.class);
        shopInfoMapper.insert(shopInfo);
        // 返回
        return shopInfo.getId();
    }

    @Override
    public void updateShopInfo(ShopInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateShopInfoExists(updateReqVO.getId());
        // 更新
        ShopInfoDO updateObj = BeanUtils.toBean(updateReqVO, ShopInfoDO.class);
        shopInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteShopInfo(Long id) {
        // 校验存在
        validateShopInfoExists(id);
        // 删除
        shopInfoMapper.deleteById(id);
    }

    private void validateShopInfoExists(Long id) {
        if (shopInfoMapper.selectById(id) == null) {
            throw exception(SHOP_INFO_NOT_EXISTS);
        }
    }

    @Override
    public ShopInfoDO getShopInfo(Long id) {
        return shopInfoMapper.selectById(id);
    }

    @Override
    public PageResult<ShopInfoDO> getShopInfoPage(ShopInfoPageReqVO pageReqVO) {
        return shopInfoMapper.selectPage(pageReqVO);
    }

}